package com.wlstock.funi.MissionInstance;

import android.content.Context;
import android.util.Log;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.em.mission.Mission;
import com.em.mission.MissionItem;
import com.em.mission.MissionListener;
import com.em.mission.MissionResultListener;
import com.wlstock.funi.app.FundApplication;
import com.wlstock.funi.constant.SharePrefConstants;
import com.wlstock.funi.constant.URLConstant;
import com.wlstock.funi.request.CustomStringRequest;
import com.wlstock.funi.request.EventBusConstants;
import com.wlstock.funi.request.NetWorkListener;
import com.wlstock.funi.request.ParseNetResponseListener;
import com.wlstock.funi.utils.CaculateUtils;
import com.wlstock.funi.utils.SharePreferenceUtils;
import com.wlstock.funi.utils.StrUtils;
import com.wlstock.funi.utils.TradeSetUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;


/**
 * Created by admin on 16/9/10.
 */

public class YiMengPositionMission extends MissionItem implements MissionListener, NetWorkListener, ParseNetResponseListener {

    private CustomStringRequest ymCWRequest;
    private String marketStaTitle ;

    private static YiMengPositionMission instance;


    public static YiMengPositionMission getInstance(Context mContex,String name) {
        if (instance == null) {
            synchronized (YiMengPositionMission.class) {
                if (instance == null) {
                    instance = new YiMengPositionMission(mContex,name);
                }
            }
        }
        return instance;
    }


    private YiMengPositionMission(Context mContext, String name) {
        super(mContext, name);
    }



    public void start(MissionResultListener resultListener){
        this.resultListener = resultListener;
        start(this, resultListener);
    }


    @Override
    public void beforeExecuteListener(Mission mission) {

    }

    @Override
    public void onExecute(Mission mission) {
        ymCWReq();
    }

    @Override
    public void afterExecute(Mission mission) {

    }


    private void ymCWReq() {
        marketStaTitle = "";
        if (ymCWRequest == null) {
            ymCWRequest = new CustomStringRequest();
        }
        ymCWRequest.networkResponseListener = this;
        ymCWRequest.request(Request.Method.GET, URLConstant.YM_HOST, null, null, URLConstant.YM_HOST, URLConstant.YM_HOST);
        ymCWRequest.parseListener = this;
    }

    @Override
    public void onErrorResponse(VolleyError error, EventBusConstants eventCode) {

    }

    @Override
    public void onErrorResponse(VolleyError error, String eventCode) {
        if (URLConstant.YM_HOST.equals(eventCode)) {
            if (resultListener != null){
                resultListener.executeFaile(YiMengPositionMission.this);
            }
        }
    }

    @Override
    public void onResponse(String response, EventBusConstants eventCode) {

    }

    @Override
    public void onResponse(String response, String eventCode) {
        if (URLConstant.YM_HOST.equals(eventCode)) {
            String parseYICWResponse = StrUtils.parseYICWResponse(response);
            Log.i("TAG",parseYICWResponse);
            if (parseYICWResponse!=null && parseYICWResponse.trim().length()>0){
                String percent = parseYICWResponse.substring(0,parseYICWResponse.length()-1);
                SharePreferenceUtils.setFloatSP(mContext, SharePrefConstants.MARKET_YIMENG_PCT, CaculateUtils.getFloatByPercent(percent));
                if (TradeSetUtils.getPosition(mContext) == -1) {
                    TradeSetUtils.setPosition(mContext, CaculateUtils.getFloatByPercent(percent));

                }
                if (parseYICWResponse!=null) {
                    String speak = "今日仓位"+parseYICWResponse;
                    marketStaTitle = speak;
                    FundApplication.getInstance().speak(speak);
                    Log.i("TAG",speak);
                }
                Log.i("TAG","益盟仓位=====");
            }
            if (resultListener!=null){
                JSONObject obj = new JSONObject();
                try {
                    obj.put("title",marketStaTitle);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                resultListener.executeSuccess(obj, YiMengPositionMission.this);
            }

        }
    }

    @Override
    public String parseResponse(NetworkResponse response) {
        String parsed;
        try {
            parsed = new String(response.data, "gb2312");
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }
        return parsed;
    }
}
